package lib

import (
	"fmt"

	"GameServer/Game/m"
)

func RewardsMails(from string, u *m.User, ids []string, PSend func(interface{}) error) (uProp *m.UpdateProp, resGoods []*m.Goods, boxResList []*m.BoxRes, err error) {
	uMail := u.GetUserMail()
	rList := uMail.GetMailByIds(ids)
	var res []m.BaseKV
	uProp = m.NewUpdateProp()

	fmt.Println("reward mails ids1111:", ids)

	ids = make([]string, 0)
	for _, item := range rList {
		// 奖励已领取或邮件删除，不能再领取奖励
		if item.Status >= 2 {
			continue
		}
		if item.Res != nil {
			ids = append(ids, item.Id.Hex())
		}
		// 奖励领取
		// // 好友给的卡片，给完卡片，把卡片塞在一个箱子里。
		// if item.Type == 2 {
		// 	lst := make([]m.BaseKV, 0)
		// 	for _, itemRes := range item.Res {
		// 		lst = append(lst, m.BaseKV{Key: itemRes.Tid, Val: itemRes.Num})
		// 	}
		// 	uPropT, resListT, _, err := GiveGoodsByKvList(from, u, lst, PSend)
		// 	if err != nil {
		// 		return nil, nil, nil, err
		// 	}
		// 	uProp.Merge(uPropT)
		// 	resGoods = append(resGoods, &m.Goods{Tid: 150241, Num: 1})
		// 	boxResT := &m.BoxRes{Tid: 150241, Res: make([]*m.Goods, 0)}
		// 	boxResT.Res = append(boxResT.Res, resListT...)
		// 	boxResList = append(boxResList, boxResT)
		// 	continue
		// }
		for _, itemRes := range item.Res {
			res = append(res, m.BaseKV{Key: itemRes.Tid, Val: itemRes.Num})
		}
	}

	uPropT, resListT, boxResListT, err := GiveGoodsByKvList(from, u, res, PSend)
	if err != nil {
		return
	}
	uProp.Merge(uPropT)
	resGoods = append(resGoods, resListT...)
	boxResList = append(boxResList, boxResListT...)

	fmt.Println("reward mails ids 2222:", ids)
	uMail.RewardsMails(ids)
	return
}
